package com.gxa.insurance.controller;

import com.gxa.insurance.pojo.Driver;
import com.gxa.insurance.pojo.DriverComments;
import com.gxa.insurance.service.DriverCommentsService;
import com.gxa.insurance.service.DriverService;
import com.gxa.insurance.service.OrderService;
import com.gxa.insurance.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName DriverController
 * @Description TODO
 * @Author ds
 * @Date 2022/6/9 14:28
 * @Version 1.0
 */
@Api
@RestController
@RequestMapping("/driver")
@CrossOrigin
public class DriverController {

    @Autowired
    DriverService driverService;

    @Autowired
    OrderService orderService;

    @Autowired
    DriverCommentsService driverCommentsService;



    @ApiOperation("添加司机的方法")
    @ApiImplicitParam(name = "driver",value = "driver",dataType = "Driver",paramType = "body")
    @PostMapping("/add")
    public Map add(@RequestBody Driver driver) throws Exception {
        driverService.add(driver);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getMap();
    }

    @ApiOperation("获取司机列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "条数",defaultValue = "10",dataType = "int",paramType = "query")
    })
    @PostMapping("/getDriverlist")
    public Map getDriverlist(Integer page,Integer size){

        Map<String, Object> driverlist = driverService.getDriverlist(page, size);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(driverlist.get("drivers"));
        jsonResult.put("count",driverlist.get("count"));
        return jsonResult.getMap();
    }


    @ApiOperation(value = "根据手机号或姓名模糊查询",produces = "body")
    @ApiImplicitParam(name = "(不传然和条件就是查询所有)查询条件字段  和分页数据  以json方式传入",value="传值示例，请保持字段名相同 " +
            "{\n" +
            "    \"name\":\"姓名或手机号\",\n" +
            "    \"page\":\"1\",\n" +
            "    \"size\":\"10\",\n" +
            "}\n")
    @PostMapping("/getLikeName")
    public Map getLikeName(@RequestBody Map<String,String> map){

        Map<String, Object> likeList = driverService.getLikeList(map);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(likeList.get("drivers"));
        jsonResult.put("count",likeList.get("count"));

        return jsonResult.getMap();
    }


    @ApiOperation(value = "修改司机状态",produces = "query")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "编号"),
            @ApiImplicitParam(name = "status",value = "司机状态")
    })
    @PostMapping("update")
    public Map update(Integer id,Integer status) throws Exception {
        driverService.update(id,status);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("更新成功");

        return jsonResult.getMap();
    }


    @ApiOperation("获取所有的订单")
    @PostMapping("/getOrder")
    public Map getOrder(){
        Map<String, List<Map<String, Object>>> order = orderService.getOrder();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(order);

        return jsonResult.getMap();
    }
    @ApiOperation(value = "司机端登录",produces = "query")
    @PostMapping("/login")
    public Map login( @RequestParam(defaultValue = "17777777777") String phone,@RequestParam(defaultValue = "666666") String pwd) throws Exception {
        Map<String, Object> login = driverService.login(phone, pwd);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("登录成功");
        jsonResult.setData(login);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "司机端个人中心-修改密码-查询用户是否存在",produces = "query")
    @PostMapping("/isExist")
    public Map isExist( String phone, String pwd) throws Exception {
        Boolean aBoolean = driverService.selectByPhoneAndPwd(phone, pwd);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(aBoolean);
        return jsonResult.getMap();
    }
    @ApiOperation(value = "司机端个人中心-修改密码",produces = "query")
    @PostMapping("/updatePwd")
    public Map updatePwd( String phone, String pwd) throws Exception {
        driverService.updatePwd(phone, pwd);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "司机端个人中心-修改手机",produces = "query")
    @PostMapping("/updatePhone")
    public Map updatePhone( String beforePhone, String nowPhone) throws Exception {
        driverService.updatePhone(beforePhone, nowPhone);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("修改成功");
        return jsonResult.getMap();
    }

    @ApiOperation(value = "获取该司机所有的评价",produces = "query")
    @ApiImplicitParam(name = "name",value = "司机姓名",dataType = "string")
    @PostMapping("/getCommentsList")
    public Map getCommentsList(String name){
        List<DriverComments> commentsList = driverCommentsService.getCommentsList(name);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功");
        jsonResult.setData(commentsList);
        return jsonResult.getMap();
    }
}
